//
//  keepInJobViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/17.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "keepInJobViewController.h"
#import "KeepInJobOtherTableViewCell.h"
#import "KeepInJobPeopleTableViewCell.h"
#import "KeepInJobModel.h"
#import "KeepInJobOtherModel.h"
#import "EntryProtectDetailVC.h"

@interface keepInJobViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrOtherData;
@property (strong, nonatomic)  NSMutableArray *arrData;
@end

@implementation keepInJobViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"保我入职";
    
    [self loadTabbleView];
    
    [self downRefreshRequest];
    [self upRefreshRequest];
}

/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    [self.tableView registerNib:[UINib nibWithNibName:@"KeepInJobOtherTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeepInJobOtherTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KeepInJobPeopleTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeepInJobPeopleTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
#pragma mark ------tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    aView.backgroundColor = UIColorFromRGB(0XF2F4F5);
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 40)];
    lab.textColor = UIColorFromRGB(0XA5B9C2);
    lab.font = [UIFont fontWithName:@"PingFangSC-Light" size:13];
    if (section == 0) {
        lab.text = @"谁能保我入职";
    }else{
        lab.text = @"试试其它公司？";
    }
    [aView addSubview:lab];
    return aView;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.arrData.count;
    }else{
        return self.arrOtherData.count;
    }
    
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    CollectionCompanyModel *model = self.arrData[indexPath.row];
    if (indexPath.section == 0) {
        KeepInJobModel *model = self.arrData[indexPath.row];
        static NSString *identifier = @"KeepInJobPeopleTableViewCell";
        KeepInJobPeopleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        [cell setDataWithModel:model];
        if (indexPath.row == (self.arrData.count-1) ){
            cell.bottomHeight.constant = 0;
        }else{
            cell.bottomHeight.constant = 19.5;
        }
        return cell;
    }else if (indexPath.section == 1){
        KeepInJobOtherModel *model = self.arrOtherData[indexPath.row];
        static NSString *identifier = @"KeepInJobOtherTableViewCell";
        KeepInJobOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        [cell setDataWithModel:model];
        return cell;
    }
    return nil;
    
}


- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        KeepInJobModel *model = self.arrData[indexPath.row];
        EntryProtectDetailVC *detail = [[EntryProtectDetailVC alloc] init];
        detail.assurePassId = [NSString stringWithFormat:@"%zd",model.incumbentAssuerPassId];
        detail.index = 2;
         detail.companyId = model.companyId;
        [self.navigationController pushViewController:detail animated:YES];
       
    }else if (indexPath.section == 1){
        KeepInJobOtherModel *model = self.arrOtherData[indexPath.row];
        EntryProtectDetailVC *detail = [[EntryProtectDetailVC alloc] init];
        detail.assurePassId = [NSString stringWithFormat:@"%zd",model.incumbentAssuerPassId];
        detail.index = 2;
        detail.companyId = model.companyId;
        [self.navigationController pushViewController:detail animated:YES];
        
    }
}
#pragma mark -
#pragma mark 下拉刷新
- (void)downRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        weakSelf.page = kStartPage;
        [YJMSendRequest postDataWithParameters:@{@"companyId":self.companyId,@"positionId":self.positionId,@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"incumbent/getIncumbentOfAssurePassByPositionId" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                self.arrOtherData = [KeepInJobOtherModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"otherCompanyIncumbents"][@"list"]];
                self.arrData = [KeepInJobModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"currentCompanyIncumbents"]];
                
                if (weakSelf.arrData.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    MJHEADER;
    
    
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 上拉加载
- (void)upRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [YJMSendRequest postDataWithParameters:@{@"companyId":self.companyId,@"positionId":self.positionId,@"pageSize":kNumOfPageSize,@"pageNo":[NSNumber numberWithInteger:weakSelf.page]} url:@"incumbent/getIncumbentOfAssurePassByPositionId" result:^(NSDictionary *result, NSError *error) {
            result = [CommonUtils decryptDicWithResult:result];
            [self hideProgress:self.view animated:YES];
            if([result[@"result"] integerValue] == 1)
            {
                NSArray *arrTemp = [KeepInJobOtherModel mj_objectArrayWithKeyValuesArray:result[@"data"][@"otherCompanyIncumbents"][@"list"]];
                [self.arrOtherData addObject:arrTemp];
                
               
                if (arrTemp.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                    
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
                [weakSelf.tableView.mj_header endRefreshing];
                
            }else{
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
                
                [CommonUtils showToast:[result objectForKey:@"msg"]];
            }
            [weakSelf.tableView reloadData];
        }];
    }];
    
    MJFOOTER;
    self.tableView.mj_footer = footer;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
